<?php

namespace App\Models;

use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;


/**
 * App\Models\Shop
 *
 * @property int $shop_id 店铺ID
 * @property string|null $shop_name 店铺名称
 * @property int $uid 店主UID
 * @property string $logo 店铺标志
 * @property int|null $type 店铺类型，1=个人店铺，2=企业店铺
 * @property string|null $phone 手机号码
 * @property string|null $province 所在省
 * @property string|null $city 所在市
 * @property string|null $district 所在县
 * @property string|null $street 街道
 * @property mixed|null $created_at 开店时间
 * @property mixed|null $updated_at 更新时间
 * @property int $views 浏览次数
 * @property int $collections 收藏数量
 * @property int $subscribes 关注量
 * @property int $orders 订单数
 * @property int $visitors 访客数
 * @property float $turnover 营业额
 * @property int $item_count 商品数
 * @property int $total_sold 累计销量
 * @property float|null $lat 纬度
 * @property float|null $lng 经度
 * @property string|null $description 店铺简介
 * @property int $main_source 主要货源
 * @property int $closed 已关闭
 * @property int $auth_state 认证状态
 * @property int $bond_state 缴纳保证金状态
 * @property int $reduce_type 减库存方式
 * @property int $seven_day_return 7天无理由退货
 * @property-read \App\Models\ShopAuth $auth
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ShopCatlog[] $catlogs
 * @property-read \App\Models\ShopContent $content
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ShopCustomer[] $customers
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ShopEvaluate[] $evaluates
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ShopSubscribe[] $fans
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\FreightTemplate[] $freightTemplates
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Item[] $items
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ShopKefu[] $kefus
 * @property-read \App\Models\RefundAddress $refundAddress
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ShopSender[] $senders
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ShopAttributes[] $shopAttributes
 * @property-read \App\Models\User $user
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop filter($input = array(), $filter = null)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop paginateFilter($perPage = null, $columns = array(), $pageName = 'page', $page = null)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop popular()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop simplePaginateFilter($perPage = null, $columns = array(), $pageName = 'page', $page = null)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereAuthState($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereBeginsWith($column, $value, $boolean = 'and')
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereBondState($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereCity($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereClosed($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereCollections($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereDescription($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereDistrict($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereEndsWith($column, $value, $boolean = 'and')
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereItemCount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereLat($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereLike($column, $value, $boolean = 'and')
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereLng($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereLogo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereMainSource($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereOrders($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop wherePhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereProvince($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereReduceType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereSevenDayReturn($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereShopId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereShopName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereStreet($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereSubscribes($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereTotalSold($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereTurnover($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereViews($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Shop whereVisitors($value)
 * @mixin \Eloquent
 */
class Shop extends Model
{
    use Filterable;

    protected $table = 'shop';
    protected $primaryKey = 'shop_id';
    protected $fillable = [
        'shop_id', 'shop_name', 'uid', 'logo', 'type', 'phone', 'province',
        'city', 'district', 'street', 'created_at', 'updated_at', 'views',
        'collections', 'subscribes', 'orders', 'visitors', 'turnover', 'item_count',
        'total_sold', 'lat', 'lng', 'description', 'main_source', 'closed', 'auth_state', 'bond_state',
        'reduce_type', 'seven_day_return'
    ];
    protected $appends = ['url', 'h5_url'];

    public static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub
        static::creating(function (Shop $shop) {
            if (!$shop->uid) $shop->uid = Auth::id();
            if (!$shop->lat) $shop->lat = 0;
            if (!$shop->lng) $shop->lng = 0;
        });
        static::created(function (Shop $shop) {
            $shop->content()->create();
            $shop->auth()->create();
        });

        static::deleted(function (Shop $shop) {
            $shop->content()->delete();
            $shop->auth()->delete();
            $shop->evaluates()->delete();
            $shop->kefus()->delete();
            $shop->customers()->delete();
            $shop->items()->get()->map(function (Item $item) {
                $item->delete();
            });
        });

        static::addGlobalScope('opening', function (Builder $builder) {
            return $builder->where('closed', 0);
        });
    }

    /**
     * @param $value
     * @return string
     */
    public function getLogoAttribute($value)
    {
        return $value ? image_url($value) : $value;
    }

    /**
     * @param $value
     */
    public function setLogoAttribute($value)
    {
        $this->attributes['logo'] = strip_image_url($value);
    }

    /**
     * @return \Illuminate\Contracts\Routing\UrlGenerator|string
     */
    public function getUrlAttribute()
    {
        return url('shop/viewshop/' . $this->attributes['shop_id'] . '.html');
    }

    /**
     * @return \Illuminate\Contracts\Routing\UrlGenerator|string
     */
    public function getH5UrlAttribute()
    {
        return url('h5/shop/viewshop/' . $this->attributes['shop_id'] . '.html');
    }

    /**
     * 按销量排序
     * @param $query
     * @return mixed
     */
    public function scopePopular(Builder $query)
    {
        return $query->orderByDesc('total_sold');
    }

    /**
     * @param $amount
     * @return int
     */
    public function incrementTotalSold($amount = 1)
    {
        return $this->increment('total_sold', $amount);
    }

    /**
     * @param int $amount
     * @return int
     */
    public function incrementViews($amount = 1)
    {
        return $this->increment('views', $amount);
    }

    /**
     * @param int $amount
     * @return int
     */
    public function incrementOrders($amount = 1)
    {
        return $this->increment('orders', $amount);
    }

    /**
     * @param $amount
     * @return mixed
     */
    public function incrementTurnover($amount = 1)
    {
        return $this->increment('turnover', $amount);
    }

    /**
     * @param int $amount
     * @return int
     */
    public function incrementVisitors($amount = 1)
    {
        return $this->increment('visitors', $amount);
    }

    /**
     * @param $amount
     * @return int
     */
    public function incrementSubscribes($amount = 1)
    {
        return $this->increment('subscribes', $amount);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function items()
    {
        return $this->hasMany(Item::class, 'shop_id', 'shop_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function auth()
    {
        return $this->hasOne(ShopAuth::class, 'shop_id', 'shop_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function content()
    {
        return $this->hasOne(ShopContent::class, 'shop_id', 'shop_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function kefus()
    {
        return $this->hasMany(ShopKefu::class, 'shop_id', 'shop_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(User::class, 'uid', 'uid');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function evaluates()
    {
        return $this->hasMany(ShopEvaluate::class, 'shop_id', 'shop_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function senders()
    {
        return $this->hasMany(ShopSender::class, 'shop_id', 'shop_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function catlogs()
    {
        return $this->hasMany(ShopCatlog::class, 'shop_id', 'shop_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function shopAttributes()
    {
        return $this->hasMany(ShopAttributes::class, 'shop_id', 'shop_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function freightTemplates()
    {
        return $this->hasMany(FreightTemplate::class, 'shop_id', 'shop_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function fans()
    {
        return $this->hasMany(ShopSubscribe::class, 'shop_id', 'shop_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function customers()
    {
        return $this->hasMany(ShopCustomer::class, 'shop_id', 'shop_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function refundAddress()
    {
        return $this->hasOne(RefundAddress::class, 'shop_id', 'shop_id');
    }
}
